#include "TreeNode.h"
#include "sstream"
#include "Collision.h"

TreeNode::TreeNode(){
	id = "";
	northWest = 0;
	northEast = 0;
	southWest = 0;
	southEast = 0;
	mBaseName = 0;
}

TreeNode::~TreeNode(){
	listObjects.clear();
	delete northWest;
	delete northEast;
	delete southWest;
	delete southEast;
}

void TreeNode::addNode(std::vector<int> param, std::vector<ObObject*> realList, int baseName){
	if(param.size()<5) return;
	std::stringstream ss;
	ss << param[0];
	if(ss.str().length()>2){
		int m2nd = atoi(ss.str().substr(1,1).c_str());
		int subint = atoi(ss.str().substr(1,ss.str().length() - 1).c_str());
		param[0] = subint;
		switch (m2nd)
		{
		case 1:
			if(northWest != 0){
				northWest->addNode(param,realList, baseName);
			}
			break;
		case 2:
			if(northEast != 0){
				northEast->addNode(param,realList, baseName);
			}
			break;
		case 3:
			if(southEast != 0){
				southEast->addNode(param,realList, baseName);
			}
			break;
		case 4:
			if(southWest != 0){
				southWest->addNode(param,realList, baseName);
			}
			break;
		default:
			return;
		}
	}else if(ss.str().length() == 2){
		//get 2nd char and cvrt to int. c++11+
		int m2nd = atoi(ss.str().substr(1,1).c_str());
		switch (m2nd)
		{
		case 1:
			if(northWest == 0){
				northWest = new TreeNode();
				TreeNode::initNode(northWest,param,realList,baseName);
			}
			break;
		case 2:
			if(northEast == 0){
				northEast = new TreeNode();
				TreeNode::initNode(northEast,param,realList,baseName);
			}
			break;
		case 3:
			if(southEast == 0){
				southEast = new TreeNode();
				TreeNode::initNode(southEast,param,realList,baseName);
			}
			break;
		case 4:
			if(southWest == 0){
				southWest = new TreeNode();
				TreeNode::initNode(southWest,param,realList,baseName);
			}
			break;
		default:
			return;
		}

	}else if(ss.str().length() == 1){
		//this node work
		TreeNode::initNode(this, param, realList, baseName);
	}else{
		//do nothing
	}
	
}

std::string TreeNode::getSaveString(){
	std::string str;
	str = std::to_string(mBaseName) +" " + std::to_string((int)pos.left) + " " + std::to_string((int)pos.top) + " " + std::to_string((int)pos.right) + " " + std::to_string((int)pos.bottom);
	for(unsigned int i  = 0; i < listObjects.size(); i++){
		if(listObjects[i]->mNeedDispose == false){
			str += " " + std::to_string(listObjects[i]->getHashId());
		}
	}
	return str;
}

void TreeNode::initNode(TreeNode* node,std::vector<int> param, std::vector<ObObject*> list, int name){
	node->pos.left = param[1];
	node->pos.top = param[2];
	node->pos.right = param[3];
	node->pos.bottom = param[4];

	node->mBaseName = name;

	for(unsigned int i = 5; i < param.size();i++){
		for(unsigned int j = 0;j<list.size();j++){
			if(param[i] == list[j]->getHashId()){
				node->listObjects.push_back(list[j]);
			}
		}
	}
}

void TreeNode::clipCamera(RECT camAABB, std::vector<ObObject*>& ob){
	
	if(Collision::checkAABBCollision(camAABB,pos)){
		if(listObjects.size() != 0){
			for(std::vector<ObObject*>::size_type i = 0; i < listObjects.size();i++){
				if(std::find(ob.begin(),ob.end(),listObjects[i]) != ob.end()){
				
				}else{
					ob.push_back(listObjects[i]);
				}
			}
		}

		if(northWest != 0){
			northWest->clipCamera(camAABB,ob);
		}

		if(northEast != 0){
			northEast->clipCamera(camAABB,ob);
		}

		if(southWest != 0){
			southWest->clipCamera(camAABB,ob);
		}

		if(southEast != 0){
			southEast->clipCamera(camAABB,ob);
		}
	}

	
}


